<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>表格管理</title>
    <link rel="stylesheet" href="/static/css/layui.css">
</head>

<body>

<!-- 主体部分 -->
<div class="layui-card">
    <div class="layui-card-body">

        <div class="layui-form">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <div class="layui-input-inline">
                        <input type="text" id="ID" name="ID" placeholder="公告编号" autocomplete="off"
                               class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <button id="btn_search" class="layui-btn layui-icon  layui-icon-search" data-type="search">查询
                    </button>
                </div>
                <div class="layui-inline">
                    <button id="btn_refresh" class="layui-btn layui-icon  layui-icon-refresh" data-type="search">重置
                    </button>
                </div>
            </div>

        </div>

        <table id="manage_table" lay-filter="manage_table"></table>
    </div>
</div>
{% if session.get("role") == "teacher" %}
<script type="text/html" id="tableToolBar">
    <div class="layui-btn-container">
        <button class="layui-btn layui-icon layui-icon-add-1" lay-event="add">新增</button>
    </div>
</script>

<!-- 表格基本操作按钮 -->
<script type="text/html" id="table_operation">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<!--其他操作-->
<script type="text/html" id="other_operation">

    </script>
{% endif %}
<!-- 添加-编辑 弹出框模板 -->
<!-- 新增,编辑表单 -->
<script type="text/html" id="add_edit_layer">

    <form class="layui-form" lay-filter="layer_form" id="layer_form" action="" style="width: 400px; margin: 10px auto">
        <div class="layui-form-item">
            <label class="layui-form-label"><span style="color: red;">*</span>编号:</label>
            <div class="layui-input-block">
                <input type="text" name="ID" lay-verify="number" lay-verType="tips" style="width: 250px"
                       placeholder="请输入编号" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label"><span style="color: red;">*</span>主题:</label>
            <div class="layui-input-block">
                <input type="text" name="headline" lay-verify="required" lay-verType="tips" style="width: 250px"
                       placeholder="请输入主题" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label"><span style="color: red;">*</span>内容:</label>
            <div class="layui-input-block">
                <input type="text" name="notice" lay-verify="required" lay-verType="tips" style="width: 250px"
                       placeholder="请输入内容" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label"><span style="color: red;">*</span>发布时间:</label>
            <div class="layui-input-block">
                <input type="datetime-local" name="notice_time" lay-verify="required" lay-verType="tips" style="width: 250px"
                       placeholder="请输入发布时间" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item" id="form_btn">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="post_form">保存</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</script>

<!-- javascript 脚本 -->
<script src="/static/layui.js"></script>
<script>
    layui.use(['layer', 'form', 'table'], function () {
        var table = layui.table;

        var
            $ = layui.jquery,
            table = layui.table,
            form = layui.form,
            layer = layui.layer;

        var currentRow = {},
            currentOpt = "",
            saveUrl = "/homepage/save",
            updateUrl = "/homepage/update",
            deleteUrl = "/homepage/delete",
            listUrl = "/homepage/list1",
            deleteId = "ID",
            layerIndex = 0;

        $(function () {
            // 查询按钮
            $("#btn_search").on("click", function () {
                table.reload('manage_table', {
                    where: {
                        ID: $("#ID").val()
                    }
                    , page: {
                        curr: 1 //重新从第 1 页开始
                    }
                });
            });
            $("#btn_refresh").on("click", function () {
                initTable();
            });
            initTable();
        });

        function initTable() {
            // 初始化列表(表格)
            table.render({
                elem: '#manage_table'
                , url: listUrl //数据接口
                , method: "GET"
                , page: true
                , toolbar: '#tableToolBar'
                , defaultToolbar: []
                , cellMinWidth: 100
                , cols: [
                    [ //表头
                        {field: 'ID', title: '编号', align: 'center', width: 100}
                        , {field: 'headline', title: '主题', align: 'center', width: 100}
                        , {field: 'notice', title: '内容', align: 'center'}
                        , {field: 'notice_time', title: '发布时间', align: 'center'}
                        , {field: 'issuer', title: '发布人', align: 'center'}
                        , {
                        width: 160,
                        align: 'center',
                        title: '操作',
                        toolbar: '#table_operation',
                        fixed: 'right'
                    }
                    ]
                ],
                parseData: function (res) { //res 即为原始返回的数据
                    console.log(res)
                    return {
                        "code": res.code == 200 ? 0 : res.code, //解析接口状态
                        "msg": res.code == 200 ? "" : res.msg, //解析提示文本
                        "count": res.count, //解析数据长度
                        "data": res.data //解析数据列表
                    };
                }
            });

            // 绑定表格bar 按钮事件
            table.on('tool(manage_table)', function (obj) {

                if (obj.event === 'edit') {
                    console.log(obj)
                    currentRow = obj.data;
                    currentOpt = updateUrl;
                    layerIndex = layer.open({
                        title: "修改",
                        type: 1,
                        shadeClose: true,
                        content: document.querySelector("#add_edit_layer").innerHTML
                    });
                    form.val('layer_form', currentRow);
                    form.render();

                } else if (obj.event === 'del') {
                    layer.confirm('确认删除该行吗？', function (index) {
                        $.ajax({
                            type: 'GET', //请求方式
                            url: deleteUrl + "/" + obj.data[deleteId], //请求接口地址
                            contentType: 'application/json;charset=utf-8',
                            success: function (result) { //针对请求结果进行处理
                                console.log(result)
                                console.log(result.code)
                                if (result.code === 0) {
                                    alert("删除成功");
                                    layer.msg("删除成功！", {time: 500});
                                    table.reload('manage_table');
                                } else {
                                    layer.msg("删除失败！");
                                }
                            }
                        });
                        layer.close(index)
                    })
                }
            });

            //监听新增事件
            table.on('toolbar(manage_table)', function (obj) {
                if (obj.event == 'add') {
                    currentRow = {};
                    currentOpt = saveUrl;
                    layerIndex = layer.open({
                        title: '新增',
                        type: 1,
                        shadeClose: true,
                        content: document.querySelector("#add_edit_layer").innerHTML
                    });
                    document.getElementById("layer_form").reset();

                    form.render();
                }

            });

        }

        // 保存按钮事件
        form.on('submit(post_form)', function (data) {
            var postData = Object.assign(currentRow, data.field);
            for (var i in postData) {    // 清除值为null的属性
                if (postData[i] === null) {
                    delete postData[i];
                }
            }
            console.log(postData)
            $.ajax({
                type: 'POST', //请求方式
                url: currentOpt, //请求接口地址
                contentType: 'application/json;charset=utf-8',
                dataType: 'json',
                data: JSON.stringify(postData), //传送数据
                success: function (result) { //针对请求结果进行处理
                    if (result.code === 0) {
                        alert("添加成功");
                        layer.msg("添加成功！", {time: 500});
                        table.reload('manage_table');

                        document.getElementById("layer_form").style.visibility='hidden'
                    } else {
                        layer.msg("添加失败！");
                    }
                }
            });
            return false;
        });
    });
</script>

</body>

</html>